/* eslint-disable @typescript-eslint/no-unused-vars */
import { useState, useEffect } from 'react';

export default function useData<T>(fetchApi: (p: any) => Promise<T>, params: any) {
  const [data, setData] = useState<Omit<T, 'success'>>(null);

  useEffect(() => {
    let igonre = false;

    fetchApi(params).then(res => {
      if (res.success && !igonre) {
        setData(res);
      }
    });

    return () => {
      igonre = true;
    };
  }, [JSON.stringify(params)]);

  return data;
}
